/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package myFileUtils;

import java.io.File;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableColumn;

/**
 *
 * @author tktung
 */
public class LoadFile implements Runnable {
    public Thread thread;
    private String folderName;
    private JTable containTable;
    private MyTableModel myTableModel;

    public LoadFile (String folder, JTable table, MyTableModel myTblModel) {
        this.folderName = folder;
        this.containTable = table;
        this.myTableModel = myTblModel;

        this.thread = new Thread (this);
        this.thread.start();
    }

    private void blankTable (MyTableModel model) {
        String [] columnName = new String []{"No", "Name", "Size"};

        Object [][] vectorData = null;
        model.setDataVector(vectorData, columnName);
        containTable.setModel(model);
        containTable.setDefaultRenderer(JLabel.class, new MyRenderer());

        TableColumn column = null;

        for (int i = 0; i < 2; i++) {
            column = containTable.getColumnModel().getColumn(i);
            if (i == 0) {
                column.setPreferredWidth(30);
            }
            else if(i == 1) {
                column.setPreferredWidth(400);
            }
            else {
                column.setPreferredWidth(15);
            }
        }
    }

    @Override
    public void run() {
//        throw new UnsupportedOperationException("Not supported yet.");

        try {
            while (true) {
                this.startLoadFile();
                this.thread.sleep(3000);
            }
        } catch (Exception ex) {
            System.err.println (ex);
        }
    }

    public void suppendLoadFile () {
        try {
            //        this.thread.suspend();
            this.thread.wait();
        } catch (InterruptedException ex) {
            Logger.getLogger(LoadFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void stopLoadFile () {
//        try {
//            this.thread.wait();
//        } catch (InterruptedException ex) {
//            Logger.getLogger(LoadFile.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    public void resumeLoadFile () {
//        this.thread.resume();
        this.thread.notify();
    }

    private void startLoadFile () {
        try {
            File file = new File(this.folderName);
            if(!file.canRead()){
                // JOptionPane.showMessageDialog(null, "Drive not found!");
                System.err.println ("Drive not found!");
            }

            File []listOfFile = file.listFiles();
            if (listOfFile != null) {
                Arrays.sort(listOfFile);


                String name; //, ext, size, date;
                ImageIcon icon = new ImageIcon();

//                if (file.getParent() != null){
//                    icon = new ImageIcon("icon\\Return.gif");
//                    Object []row = {"", new JLabel("[...]", icon, SwingConstants.LEFT), ""};
//                    model.addRow(row);
//                }
//
//                for (int i = 0; i < danhSach.length; i++){
//                    if (danhSach[i].isDirectory()){
//                        name = danhSach[i].getName();
//                        ext = "";
//                        size = "<DIR>";
//                        //date = simdf.format(danhSach[i].lastModified());
//
//                        if (!danhSach[i].isHidden()){
//                            icon = new ImageIcon("icon\\Folder.gif");
//                        }else{
//                            icon = new ImageIcon("icon\\Folder_Hide.gif");
//                        }
//
//    //                    Object []row = {new JLabel(name, icon, SwingConstants.LEFT), ext, new JLabel(size, SwingConstants.CENTER), date};
//
//                       Object []row = {"", new JLabel("[...]", icon, SwingConstants.LEFT), ""};
//                        model.addRow(row);
//                    }
//                }


                blankTable(this.myTableModel);
                for (int i = 0; i < listOfFile.length; i++){
                    if (listOfFile[i].isFile()){
                        name = listOfFile[i].getName();

//                        if (!danhSach[i].isHidden()){
//                            icon = new ImageIcon("icon\\File.gif");
//                        }else{
//                            icon = new ImageIcon("icon\\File_Hide.gif");
//                        }

                        sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(listOfFile[i]);
                        icon = new ImageIcon(sf.getIcon(true).getScaledInstance(16, 14, 0));

//                        Object []row = {new JLabel(name, icon, SwingConstants.LEFT), ext, new JLabel(size, SwingConstants.RIGHT), date};

                        Object []row = {i +1,
                                        new JLabel(name, icon, SwingConstants.LEFT),
                                        listOfFile [i].length()};
                        this.myTableModel.addRow(row);
                    }
                }
            }
        }catch(Exception ex){
            System.err.println (ex);
            //JOptionPane.showMessageDialog(null, "Sorry, I can't open this folder");
            System.err.println ("Sorry, I can't open this folder");
        }
    }

}
